package com.qf.controller;


import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.service.DtsOrderGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.OrderGoodsVo;
import com.qf.vo.OrderVo;
import com.qf.vo.StatVo;

import com.qf.vo.UserVoTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/admin/stat")
public class DtsAdminStatController {
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsOrderService orderService;
    @Autowired
    DtsOrderGoodsService orderGoodsService;

    @RequestMapping("/user")
    public Object userStat(){
        Map<String,Object> data = new HashMap<>();
        StatVo statVo = new StatVo();
        String[] strings = new String[]{"day","users"};
        statVo.setColumns(strings);
        List<UserVoTest> userList=userService.findUser();
//        List<Map> list = new ArrayList<>();
        for (UserVoTest userVoTest : userList) {
            Map<String,Object> map = new HashMap<>();
            map.put("day",userVoTest.getDay());
            map.put("users",userVoTest.getUsers());
            statVo.add(map);
        }
//        statVo.setRows(list);


        data.put("columns",statVo.getColumns());
        data.put("rows",statVo.getRows());
        return ResponseUtil.ok(data);
    }


    @RequestMapping("/order")
    public Object order(){
        Map<String,Object> data = new HashMap<>();
        StatVo statVo = new StatVo();
        String [] columns= new String[] {"day","orders","customers","amount","pcr"};
        statVo.setColumns(columns);
        List<OrderVo> orderlist=orderService.findOrder();
        for (OrderVo orderVo : orderlist) {
            Map<String,Object> map = new HashMap<>();
            map.put("amount",orderVo.getAmount());
            map.put("orders",orderVo.getOrders());
            map.put("customers",orderVo.getCustomers());
            map.put("day",orderVo.getDay());
            map.put("pcr",orderVo.getPcr());
            statVo.add(map);
        }

        data.put("columns",statVo.getColumns());
        data.put("rows",statVo.getRows());
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/goods")
    public Object goods(){
        Map<String,Object> data = new HashMap<>();
        StatVo statVo = new StatVo();
        String[] columns = new String[]{"day","orders","products","amount"};
        statVo.setColumns(columns);
        List<OrderGoodsVo> orderGoodsVos=orderGoodsService.findOrderGoods();
        List<Map> list = orderGoodsVos.stream().map(orderGoodsVo -> {
            Map map = new HashMap();
            map.put("amount", orderGoodsVo.getAmount());
            map.put("orders", orderGoodsVo.getOrders());
            map.put("day", orderGoodsVo.getDay());
            map.put("products", orderGoodsVo.getProducts());
            return map;
        }).collect(Collectors.toList());


        statVo.setRows(list);

        data.put("columns",statVo.getColumns());
        data.put("rows",statVo.getRows());
        return ResponseUtil.ok(data);
    }
}
